package br.com.sistema.models;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;

@Entity
@Table(name="municipio", uniqueConstraints={@UniqueConstraint(columnNames={"estaid","municodnum"})})
public class Municipio {

	@ManyToOne
	@JoinColumn(name="estaid", nullable=false)
	private Estado estado;
	
	@Id
	@Column(name="muniid")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer muniid;
	
	@Column(name="municodnum", length=10)
	private String municodnum;
	
	@Column(name="muninome", length=50, nullable=false)
	private String muninome;
	
	@Column(name="xdatai", nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Calendar xdatai;
	
	@Column(name="xdatau", nullable=false)
	@Temporal(TemporalType.TIMESTAMP)
	private Calendar xdatau;
	
	@Column(name="xusuario", nullable=false)
	private String xusuario;

	@Version
	@Column(name="xversao")
	private Integer xversao;

	@PrePersist
	public void prePersist(){
		xdatai = Calendar.getInstance();
		xdatau = Calendar.getInstance();
	}
	
	@PreUpdate
	public void preUpdate(){
		xdatau = Calendar.getInstance();
	}
	
	@Transient
	private String estanome;
	
	@Transient
	private String paisnome;
	
	public String getEstanome() {
		return getEstado().getEstanome();
	}
	
	public void setEstanome(String estanome) {
		this.estanome = getEstado().getEstanome();
	}
	
	public String getPaisnome() {
		return this.estado.getPais().getPaisnome();
	}

	public Estado getEstado() {
		return estado;
	}

	public void setEstado(Estado estado) {
		this.estado = estado;
	}

	public Integer getMuniid() {
		return muniid;
	}

	public void setMuniid(Integer muniid) {
		this.muniid = muniid;
	}

	public String getMunicodnum() {
		return municodnum;
	}

	public void setMunicodnum(String municodnum) {
		this.municodnum = municodnum;
	}

	public String getMuninome() {
		return muninome;
	}

	public void setMuninome(String muninome) {
		this.muninome = muninome;
	}

	public Calendar getXdatai() {
		return xdatai;
	}

	public void setXdatai(Calendar xdatai) {
		this.xdatai = xdatai;
	}

	public Calendar getXdatau() {
		return xdatau;
	}

	public void setXdatau(Calendar xdatau) {
		this.xdatau = xdatau;
	}

	public String getXusuario() {
		return xusuario;
	}

	public void setXusuario(String xusuario) {
		this.xusuario = xusuario;
	}
	
}
